package com.example.tvmatchddemo;


import static com.example.tvmatchddemo.util.DebugLog.LOG;

import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;


import com.example.tvmatchddemo.custom.MarqueeText;
import com.example.tvmatchddemo.custom.TvTabLayout;
import com.example.tvmatchddemo.fragment.MessageFragment;
import com.example.tvmatchddemo.fragment.MusicFragment;
import com.example.tvmatchddemo.fragment.PhotoFragment;
import com.example.tvmatchddemo.fragment.SettingFragment;
import com.example.tvmatchddemo.fragment.VideoFragment;
import com.example.tvmatchddemo.fragment.WoxiuFragment;
import com.example.tvmatchddemo.util.DebugLog;

import java.util.ArrayList;
import java.util.List;

public class HomecNoViewpagertivity extends AppCompatActivity {
    private static final String TAG = HomecNoViewpagertivity.class.getSimpleName();
    private TvTabLayout mTabLayout;
    private List<String> list;

    private MarqueeText marqueeText;
    private List<Fragment> fragments;
    private FrameLayout frameLayout;
    private Fragment currentFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home_no_viewpager);
        mTabLayout = findViewById(R.id.tablayout);
        frameLayout = findViewById(R.id.home_tab_container);
        marqueeText = findViewById(R.id.tvMarquee);
        initDynamicList();
//        viewPager.setScanScroll(false);
        iniData();
        ininView();
        initEvent();
    }

    private void initEvent() {
        mTabLayout.addOnTabSelectedListener(new TvTabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TvTabLayout.Tab tab) {
                DebugLog.d(LOG + TAG + " ---- onTabSelected " + tab.getPosition());
                final int position = tab.getPosition();
                currentFragment = getSupportFragmentManager().findFragmentByTag(position + "");
                FragmentTransaction mFt = getSupportFragmentManager().beginTransaction();

                if (currentFragment == null) {
                    currentFragment = fragments.get(position);
                    mFt.add(R.id.home_tab_container, currentFragment, String.valueOf(position));
                } else {
                    mFt.show(currentFragment);
                }
                mFt.commit();


            }

            @Override
            public void onTabUnselected(TvTabLayout.Tab tab) {
                DebugLog.d(LOG + TAG + " ---- onTabUnselected " + tab.getPosition());
                if (currentFragment != null) {
                    FragmentTransaction mFt = getSupportFragmentManager().beginTransaction();
                    mFt.hide(currentFragment);
                    mFt.commit();
                }
            }

            @Override
            public void onTabReselected(TvTabLayout.Tab tab) {
                DebugLog.d(LOG + TAG + " ---- onTabReselected " + tab.getPosition());
            }
        });
        mTabLayout.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    mTabLayout.setIndicatorDrawable(ContextCompat.getDrawable(HomecNoViewpagertivity.this, R.mipmap.zx_p1_light));
                } else {
                    mTabLayout.setIndicatorDrawable(ContextCompat.getDrawable(HomecNoViewpagertivity.this, R.color.transparent));
                }
            }
        });

    }

    private void iniData() {
        list = new ArrayList<>();
        list.add("我秀沃家");
        list.add("相册");
        list.add("音乐");
        list.add("影视");
        list.add("消息");
        list.add("设置");

        fragments = new ArrayList<>();
        fragments.add(new WoxiuFragment());
        fragments.add(new PhotoFragment());
        fragments.add(new MusicFragment());
        fragments.add(new VideoFragment());
        fragments.add(new MessageFragment());
        fragments.add(new SettingFragment());
    }

    private void ininView() {
        // 代码动态添加tab
        for (int i = 0; i < list.size(); i++) {
            if (i == 0) {
                mTabLayout.addTab(mTabLayout.newTab().setText(list.get(i)), true);
            } else {
                mTabLayout.addTab(mTabLayout.newTab().setText(list.get(i)));
            }

        }
    }
    /**
     * 初始化底部广播最新动态数据
     */
    private void initDynamicList() {
        String space = "\u3000\u3000\u3000\u3000";
        marqueeText.setText(getString(R.string.mess1) + space + getString(R.string.mess2) + space + getString(R.string.mess3));
    }

}
